//
//  HorizonScrollView.m
//  ScrollConnectDemo
//
//  Created by Glen Eller on 2017/11/20.
//  Copyright © 2017年 none. All rights reserved.
//

#import "HorizonScrollView.h"

#import "HorizonScrollSubView.h"

static CGFloat viewSpace = 5.f;

@interface HorizonScrollView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIPageControl * pageControl;

@property (nonatomic, strong) NSMutableArray<HorizonScrollSubView *> *viewsArray;//存储三个子视图

@end

@implementation HorizonScrollView

#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.currentPage = 0;
        
        //scrollView
        [self addSubview:self.scrollView];
        //        [self initPartViewsCount:PageCount andViewPercent:viewPercent];
        //pageControl
        [self addSubview:self.pageControl];
        
    }
    return self;
}

//创建滑动视图
- (void)initPartViewsCount:(NSInteger)subViewsCount andViewPercent:(CGFloat)percent
{
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[HorizonScrollSubView class]]) {
            [view removeFromSuperview];
        }
    }
    [self.viewsArray removeAllObjects];
    
    CGFloat partViewWidth = self.bounds.size.width * percent;
    
    for (int i = 0; i<subViewsCount; i++) {
        
        CGFloat viewX = viewSpace*0.5*(i+1) + (partViewWidth + viewSpace)*i;
        
        CGRect partFrame = CGRectMake(viewX, 0, partViewWidth, kHorizonRelationH);
        HorizonScrollSubView *partView = [self generalCommonSubviewWithFrame:partFrame];
        //        partView.backgroundColor = colorsAry[i];
        partView.tag = 200 + i;
        NSDictionary *titleDic = self.datasArray[i];
        self.levelNum = titleDic[@"levelNum"];
        partView.showStr = titleDic[@"title"];
        [self.scrollView addSubview:partView];
        [self.viewsArray addObject:partView];
    }
    
    CGFloat scrollMaxWidth = (self.bounds.size.width * percent + viewSpace)* subViewsCount;
    self.scrollView.contentSize = CGSizeMake(scrollMaxWidth, self.bounds.size.height);
    
}

- (HorizonScrollSubView *)generalCommonSubviewWithFrame:(CGRect)frame
{
    HorizonScrollSubView *relationView = [[HorizonScrollSubView alloc] initWithFrame:frame];
    
    return relationView;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    NSLog(@"拖动");
    
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat scrollViewWidth = self.scrollView.bounds.size.width;
    NSInteger temp = (int)offsetX / (int)scrollViewWidth;
    
    CGFloat lastPartViewOffsetX = scrollViewWidth * (self.datasArray.count - 1);
    if (offsetX == lastPartViewOffsetX) {
        temp = self.datasArray.count - 1;
    }
    
    if (temp != self.currentPage) {

        NSLog(@"temp==%ld",temp);
        if (self.ScrollingBlock) {
            self.ScrollingBlock(self.levelNum,temp);
        }
    }
    
    self.currentPage = temp;
    self.pageControl.currentPage = temp;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速---------------");
//    CGFloat offsetX = scrollView.contentOffset.x;
//    CGFloat scrollViewWidth = self.scrollView.bounds.size.width;
//    NSInteger temp = (int)offsetX / (int)scrollViewWidth;
//
//    CGFloat lastPartViewOffsetX = scrollViewWidth * (self.datasArray.count - 1);
//    if (offsetX == lastPartViewOffsetX) {
//        temp = self.datasArray.count - 1;
//    }
//
//    if (temp != self.currentPage) {
//
//        NSLog(@"temp==%ld",temp);
//        if (self.ScrollingBlock) {
//            self.ScrollingBlock(temp);
//        }
//    }
}


/**
 赋值
 
 @param currentPage :
 */
- (void)setPartViewData:(NSInteger)currentPage
{
    
    if (!self.datasArray || self.datasArray.count == 0) {
        return;
    }
    if (!self.viewsArray || self.viewsArray.count == 0) {
        return;
    }
    
//    int i = 0;
//    for (CollectDetailMainAccountsModel *model in self.datasArray) {
//        CollectRelationSubView *tempView = self.viewsArray[i];
//        [tempView configCellWithData:model];
//        i++;
//    }
}

- (void)setDatasArray:(NSArray *)datasArray
{
    _datasArray = datasArray;
    
    self.pageControl.numberOfPages = datasArray.count;
    
    if (self.viewsArray.count != datasArray.count) {
        [self refreshViewsPgeNumber];
    }
    [self setPartViewData:self.currentPage];
}

/**
 刷新界面 防止子视图少于3个
 */
- (void)refreshViewsPgeNumber
{
    NSInteger pageCount = self.datasArray.count;
    CGFloat percent = viewPercent;
    
    if (pageCount == 1) {
        
        percent = 1;
        self.scrollView.scrollEnabled = NO;
//        self.pageControl.hidden = YES;
        
    }
    [self initPartViewsCount:pageCount andViewPercent:percent];
    
}


#pragma mark - 创建视图

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        CGFloat scrollWidth = self.bounds.size.width * viewPercent + viewSpace;
        
        CGRect scrollFrame = CGRectMake(0, 0, scrollWidth, kHorizonRelationH);
        _scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
        _scrollView.backgroundColor = [UIColor clearColor];
        
        _scrollView.layer.masksToBounds = NO;
        
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.directionalLockEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        
        _scrollView.delegate = self;
        
        _scrollView.contentOffset = CGPointMake(0, 0);
    }
    return _scrollView;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        CGFloat pageWidth = self.bounds.size.width;
        CGFloat pageHeight = kHorizonScrollH - kHorizonRelationH;
        CGFloat pageMinY = kHorizonRelationH - 5.f;
        CGFloat pageMinX = 0;
        CGRect pageFrame = CGRectMake(pageMinX, pageMinY, pageWidth, pageHeight);
        _pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
        [_pageControl setBackgroundColor:[UIColor clearColor]];
        _pageControl.transform = CGAffineTransformMakeScale(0.8, 0.8);
        
        _pageControl.currentPage = self.currentPage;
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}

- (void)setFrame:(CGRect)frame
{
    frame.size.height = kHorizonScrollH - 10.f;
    [super setFrame:frame];
}

#pragma mark - 初始化

- (NSMutableArray<HorizonScrollSubView *> *)viewsArray
{
    if (!_viewsArray) {
        _viewsArray = [NSMutableArray array];
    }
    return _viewsArray;
}

@end
